package cn.jiyun.function.controller;

import cn.jiyun.Result.Result;
import cn.jiyun.function.service.DriverService;
import cn.jiyun.pojo.function.Count;
import cn.jiyun.pojo.function.Driver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/driver")
@Slf4j
public class DriverController {
    @Autowired
    private DriverService driverService;

    @GetMapping("updateVerificationStatus")
    public Result updateVerificationStatus(Integer userId, Integer status) {
        driverService.updateVerificationStatus(userId, status);
        return Result.ok();
    }

    @PostMapping("/findPage")
    public Result findPage(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
                           @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize,
                           @RequestBody Driver driver) {
        log.info("driver: {}", driver);
        return Result.ok(driverService.findPage(pageNum, pageSize, driver));
    }

    @GetMapping("/findPage1")
    public Result findPage1(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
                            @RequestParam(defaultValue = "10", name = "pageSize") Integer pageSize
    ) {
        return Result.ok(driverService.findPage1(pageNum, pageSize));
    }

    @GetMapping("/findById")
    public Result findById(Integer userId) {
        Driver user = driverService.findById(userId);
        return Result.ok(user);
    }


    @PostMapping("/findCount")
    public Result findCount(@RequestBody Count count1) {
        List<Count> countList = driverService.findCount(count1);
        List<Integer> newList = new ArrayList<>();
        for (Count count : countList) {
            newList.add(Integer.parseInt(count.getCount()));
        }
        log.info("count1: {}", count1);
        log.info("newList: {}", newList);
        return Result.ok(newList);
    }

    @PostMapping("/findCount1")
    public Result findCount1(@RequestBody Count count2) {
        List<Count> countList = driverService.findCount1(count2);
        ArrayList<LocalDate> count = new ArrayList<>();
        for (Count count1 : countList) {
            count.add(count1.getRegistrationTime());
        }
        log.info("count2: {}", count2);
        log.info("count: {}", count);
        return Result.ok(count);
    }
}
